package com.sora.sys.controller;


import com.sora.basic.util.AjaxResult;
import com.sora.basic.util.PageList;
import com.sora.sys.domain.DictionaryDetail;
import com.sora.sys.query.DictionaryDetailQuery;
import com.sora.sys.service.IDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：部门
 */
@RestController
@RequestMapping("/dictionaryDetail")
@Api(value="部门接口类",description = "处理部门的CRUD操作")
public class DictionaryDetailController {

    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    /**
     * 查询单个
     * @param id
     * @return
     */
    //http://localhost:8080/dictionaryDetail/1
    @GetMapping("/{id}")
    @ApiOperation(value = "接口：查询单个")
    public DictionaryDetail findOne(@PathVariable("id")Long id){
        return dictionaryDetailService.loadById(id);
    }

    /**
     * 查询所有
     * @return
     */
    //http://localhost:8080/dictionaryDetail
    @GetMapping
    @ApiOperation(value = "接口：查询所有")
    public List<DictionaryDetail> findAll(){
        return dictionaryDetailService.loadAll();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "接口：删除")
    public AjaxResult del(@PathVariable("id")Long id){
        try {
            dictionaryDetailService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加或者修改
     * @param dictionaryDetail
     * @return
     */
    @PutMapping
    @ApiOperation(value = "接口：添加或者修改")
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail){
        try {
            if(dictionaryDetail.getId() == null){
                dictionaryDetailService.save(dictionaryDetail);
            }else{
                dictionaryDetailService.update(dictionaryDetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 分页查询+高级查询
     * @param dictionaryDetailQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "接口：分页查询+高级查询")
    public PageList<DictionaryDetail> pageQuery(@RequestBody DictionaryDetailQuery dictionaryDetailQuery){
        return dictionaryDetailService.pageQuery(dictionaryDetailQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "接口：批量删除")
    public AjaxResult del(@RequestBody List<Long> ids){
        try {
            dictionaryDetailService.batchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }


}
